(function($){

	function Presenter() {
		this.titleElement = null;
		this.headingElement = null;
		this.containerElement = null;
		this.modalElement = null;
		this.validationElementClass = null;
		this.views = new Array();
	}

	/**************************
	 * Initialization
	 **************************/

	Presenter.prototype.initialize = function() {
		controller.log ("JSMVPC ERROR: Presenter.initialize() needs to be overridden");
	}

	Presenter.prototype.run = function () {
		controller.debug ("Presenter.run()");
		presenter.initialize();
		presenter.emptyContainer();
		presenter.emptyModal();
	}

	/**************************
	 * Page Setup
	 **************************/

	Presenter.prototype.setTitleElement = function(titleElement) {
		if (titleElement.length >= 1) {
			presenter.titleElement = titleElement;
		} else {
			controller.log ("JSMVPC ERROR: Presenter.setTitle() called with null element");
		}
	}

	Presenter.prototype.setHeadingElement = function(headingElement) {
		if (headingElement.length >= 1) {
			presenter.headingElement = headingElement;
		} else {
			controller.log ("JSMVPC ERROR: Presenter.setHeading() called with null element");
		}
	}

	Presenter.prototype.setContainerElement = function(containerElement) {
		if (containerElement.length >= 1) {
			presenter.containerElement = containerElement;
		} else {
			controller.log ("JSMVPC ERROR: Presenter.setContainer() called with null element");
		}
	}

	Presenter.prototype.setModalElement = function(modalElement) {
		if (modalElement.length >= 1) {
			presenter.modalElement = modalElement;
			presenter.modalElement.jqm();
		} else {
			controller.log ("JSMVPC ERROR: Presenter.setModal() called with null element");
		}
	}


	/* ----------------------- */

	Presenter.prototype.setTitle = function(title) {
		presenter.titleElement.text(title);
	}

	Presenter.prototype.setHeading = function(title) {
		presenter.headingElement.text(title);
	}


	/**************************
	 * View Management
	 **************************/

	Presenter.prototype.emptyContainer = function() {
		this.containerElement.empty();
	}

	Presenter.prototype.show404 = function(view) {
		presenter.showView(view, true);
	}

	Presenter.prototype.showView = function(view) {

		if (presenter.views[view] != null) {
			presenter.containerElement.empty();
			presenter.containerElement.append(presenter.views[view]);
			presenter.displayErrors();
			controller.mapEvents();
		} else {
			$.ajax({
				"url":"views/" + view,
				"success": function(html) {
					presenter.views[view] = html;
					presenter.containerElement.empty();
					presenter.containerElement.append(html);
					presenter.displayErrors()
					controller.mapEvents();
				},
				"error":function(error) {
					controller.debug ("404 Error");
					presenter.handle404();
				}
			});
		}
	}

	/**************************
	 * Modals
	 **************************/

	Presenter.prototype.setModalContent = function(content) {
		presenter.modalElement.empty();
		presenter.modalElement.html(content);
	}
	 
	Presenter.prototype.showModal = function() {
		presenter.modalElement.jqmShow();
	}
	 
	Presenter.prototype.hideModal = function() {
		presenter.modalElement.jqmHide();
	}

	Presenter.prototype.emptyModal = function() {
		this.modalElement.empty();
	}

	/**************************
	 * Validation
	 **************************/

	Presenter.setValidationElementClass = function(validationElementClass) {
		this.validationElementClass = validationElementClass;
	}

	Presenter.displayErrors = function(){

		var messages = model.getErrorMessages();

		if (messages.length > 0) {
			var messageString = "<ul>";

			for (var message in messages) {
				messageString += "<li>" + message + "</li>";
			}

			messageString += "</ul>";

			$("." + this.validationElementClass).empty();
			$("." + this.validationElementClass).append(messageString);
		}
	}


	/**************************
	 * Helpers
	 **************************/

	Presenter.prototype.handle404 = function() {

		controller.debug (typeof controller.actions['404']);

		if ((typeof controller.actions['404'] == "function") && controller.check404SafetyOk() == true) {
			controller.actions['404']();
		} else {
			presenter.emptyContainer();
			controller.log ("JSMVP ERROR: A 404 error occured. JSMVP not correctly configured");
		}
	}

	/**************************
	 * For office use only
	 **************************/

	window.presenter = new Presenter();
	return(window.presenter);

})(jQuery);